//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;

namespace Mindscape.BackgroundMotion.Website.Infrastructure
{
  /// <summary>
  /// This class manages loading the configuration to describe external providers plugins (e.g. Flickr) which are registered with the system
  /// </summary>
  public class ExternalProvidersConfigSectionHandler : IConfigurationSectionHandler
  {
    #region IConfigurationSectionHandler Members

    /// <summary>
    /// Creates a configuration section handler.
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="configContext">Configuration context object.</param>
    /// <param name="section"></param>
    /// <returns>The created section handler object.</returns>
    public object Create(object parent, object configContext, XmlNode section)
    {
      Dictionary<string, Type> providers = new Dictionary<string, Type>();

      foreach (XmlElement providerNode in section.SelectNodes("provider"))
      {
        Type providerType = Type.GetType(providerNode.GetAttribute("type"));
        if (providerType == null || providerType.GetInterface("Mindscape.BackgroundMotion.Core.Providers.IResourceProvider") == null) continue;

        providers.Add(providerNode.GetAttribute("name"), providerType);
      }

      return providers;
    }

    #endregion
  }
}
